﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CHPM.IC.ServiceLocator;
using CHPM.PL.DesktopUI.Interfaces;
using CHPM.PL.DesktopUI.ServiceReference1;

namespace CHPM.PL.DesktopUI.Presenters
{
    public class MainGridPresenter:BasePresenter
    {
        private IMainService _mainService;
        private IMainGrid _mainGridView;

        public MainGridPresenter(IServiceLocator serviceLocator, IMainGrid mainGridView ) : base(serviceLocator)
        {
            _mainService = serviceLocator.Resolve<IMainService>();
            _mainGridView = mainGridView;
            _mainGridView.OnGetPatients += SendPatientsToView;
        }

        void SendPatientsToView()
        {
            List<PATIENT> patients = new List<PATIENT>();
            patients = _mainService.GetAllPatients().ToList();
            _mainGridView.SetDataSource(patients);
        }

        public void PresentMainGrid(Control container)
        {
            this.ClearControls(container);
            container.Controls.Add(_mainGridView.AsControl);
            container.Controls[container.Controls.Count - 1].Dock = DockStyle.Fill;
        }

        public int GetSelectedItem()
        {
            DataGridView grid = _mainGridView.AsControl.Controls[0] as DataGridView;
            foreach (DataGridViewRow row in grid.Rows)
            {
                if (row.Selected)
                {
                    return Convert.ToInt32(row.Cells[0].Value);
                }
            }
            return -1;
        }
    }
}
